package com.liang.reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Text {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取类的字节码文件对象
        Class cla = Class.forName("com.liang.reflectdemo.Student");

        //获得公共构造方法创建对象
        Constructor con1 = cla.getConstructor(String.class, int.class);

        //创建对象
        Object obj1 = con1.newInstance("zhangsna", 23);

        System.out.println(obj1);

        //获取私有的无参构造方法
        Constructor con2 = cla.getDeclaredConstructor();

        //暴力使用
        con2.setAccessible(true);

        //无参创建对象
        Object obj2 = con2.newInstance();
        System.out.println(obj2);

    }
}
